package com.simas.common;

import java.util.Calendar;
import java.util.Date;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.io.MalformedURIException;
import net.rim.device.api.io.URI;
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;

public class CommonFunction {
//	// resize to fit screen
//	public static Bitmap resizeBitmap(Bitmap image, int width, int height) {	
//		//Need an array (for RGB, with the size of original image)
//		int rgb[] = new int[image.getWidth()*image.getHeight()];
//
//		//Get the RGB array of image into "rgb"
//		image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
//
//		//Call to our function and obtain RGB2
//		int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
//
//		//Create an image with that RGB array
//		Bitmap temp2 = new Bitmap(width, height);
//
//		temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
//		
//		return temp2;
//	}
//	private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {
//		int out[] = new int[x2*y2];
//		for (int yy = 0; yy < y2; yy++) {
//			int dy = yy * y / y2;
//			for (int xx = 0; xx < x2; xx++) {
//				int dx = xx * x / x2;
//				out[(x2 * yy) + xx] = ini[(x * dy) + dx];
//			}
//		}
//		return out;
//	}
	
	// resize, based on display width
	public Bitmap getScaledBitmapImage(String imagename) {
        EncodedImage image = EncodedImage.getEncodedImageResource(imagename); 
        
        int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
        
        // perbandingan image asli
        //double dblRatio = image.getHeight() / image.getWidth();
        
        // perhitungan ukuran akhir
        double dblDispWidth = Display.getWidth();
        double dblDispHeight = (image.getHeight() * Display.getWidth()) / image.getWidth();
        int intNewWidth = (int) dblDispWidth;
        int intNewHeight = (int) dblDispHeight;
        
        int requiredWidthFixed32 = Fixed32.toFP(intNewWidth);
        int requiredHeightFixed32 = Fixed32.toFP(intNewHeight);
        
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
        
        image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
        
        return image.getBitmap();
    }
	
	public String fnStringToURL(String strSource) {
		URI myURI = null;
		try {
			myURI = URI.create(strSource);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String myURL = myURI.toString();
		return myURL;
	}
	
	public String getTodayDate(String strFormat) {
		String strTodayDate;
		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		Calendar cal = Calendar.getInstance();
		Date dtToday = cal.getTime();
		strTodayDate = sdf.format(dtToday);
		return strTodayDate;
	}
	
	public String ddMMMyyyy2ddMMyyyy(String strInputDate) {
		String strResult;
		
		String strDate = strInputDate.substring(0,2);
		String strMonth = strInputDate.substring(3,6);
		String strNumMonth = getMonthInNum(strMonth);
		String strYear = strInputDate.substring(7,11);
		strResult = strDate + "/" + strNumMonth + "/" + strYear;
		
		return strResult;
	}
	
	public String getMonthInNum(String MonthInWord) {
		String strResult;
		
		if (MonthInWord.equalsIgnoreCase("Jan")) {
			strResult = "01";
		} else if (MonthInWord.equalsIgnoreCase("Feb")) {
			strResult = "02";
		} else if (MonthInWord.equalsIgnoreCase("Mar")) {
			strResult = "03";
		} else if (MonthInWord.equalsIgnoreCase("Apr")) {
			strResult = "04";
		} else if (MonthInWord.equalsIgnoreCase("May")) {
			strResult = "05";
		} else if (MonthInWord.equalsIgnoreCase("Jun")) {
			strResult = "06";
		} else if (MonthInWord.equalsIgnoreCase("Jul")) {
			strResult = "07";
		} else if (MonthInWord.equalsIgnoreCase("Aug")) {
			strResult = "08";
		} else if (MonthInWord.equalsIgnoreCase("Sep")) {
			strResult = "09";
		} else if (MonthInWord.equalsIgnoreCase("Oct")) {
			strResult = "10";
		} else if (MonthInWord.equalsIgnoreCase("Nov")) {
			strResult = "11";
		} else {
			strResult = "12";
		}
		
		return strResult;
	}

}
